<%@ page import="java.sql.Connection" %>
<%@ page import="java.sql.DriverManager" %>
<%@ page import="java.sql.PreparedStatement" %>
<%@ page import="java.sql.ResultSet" %><%--
  Created by IntelliJ IDEA.
  User: XiangLang15078435014
  Date: 2021/11/1
  Time: 20:50
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" pageEncoding="UTF-8" %>
<html>
    <head>
        <title>Title</title>
    </head>
    <body>

        <%--<%
            /*//不可以加
            response.setCharacterEncoding("utf-8");
            request.setCharacterEncoding("utf-8");
            */

            //调用request的方法获取前端传输的数据
            String sname = request.getParameter("sname");
            sname = new String(sname.getBytes("ISO-8859-1"), "UTF-8");
            String name = new String(request.getParameter("sname").getBytes("iso-8859-1"), "utf-8");
            String password = request.getParameter("password");
            out.print(sname + name + password);
        %>--%>

        <%--使用JavaBean获取前端输出的数据
                把前端数据存入到javaBean中
        --%>


        <%
            request.setCharacterEncoding("utf8");
            response.setCharacterEncoding("utf8");
        %>
        <jsp:useBean id="student" class="com.edu.oracle.entity">
            <jsp:setProperty name="student" property="sno" param="name"></jsp:setProperty>
            <jsp:setProperty name="student" property="password"></jsp:setProperty>
        </jsp:useBean>


        <%--把数据从javaBean中取出来--%>
        <jsp:getProperty name="student" property="sno"/>
        <jsp:getProperty name="student" property="password"/>

        <%
            //1.加载驱动
            Class.forName("oracle.jdbc.driver.OracleDriver");
            String url="jdbc:oracle:thin:@localhost:1521:orcl";
            String user="scott";
            String pass="oracle";
            //2.创建连接
            Connection connection = DriverManager.getConnection(url,user,pass);

            //3.sql语句
            String sql ="select * from student where sno=? and password=?";
            //4.创建操作对象
            PreparedStatement pstm = connection.prepareStatement(sql);
            pstm.setString(1,student.getSno());
            pstm.setString(2,student.getPassword());
            ResultSet rs = pstm.executeQuery();

            //5.处理查询结构级
            if(rs!=null){
                System.out.println("查询成功");
                //request.setAttribute("sname",rs.getString("sname"));
                //request.getRequestDispatcher("index.html").forward(request,response);
                response.sendRedirect("index.html");
            }
            //6.关闭连接
            if (rs!=null){
                rs.close();
            }
            if (pstm!=null){
                pstm.close();
            }
            if (connection!=null){
                connection.close();
            }

        %>


    </body>
</html>
